// pages/workInspectDetail/workInspectDetail.js
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    loading: 1
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    let that = this;
    this.setData({
      inspectPlanId: options.inspectPlanId,
      hide: 0,
      categoryId: options.categoryId
    })
  },

  onShow: function() {
    this.setData({
      hide: 0
    })
    wx.request({
      url: app.inspectUrl + '/WeChat/weChatSystem/getInspectInfoByPlanId',
      method: 'POST',
      data: this.data.inspectPlanId,
      dataType: 'json',
      success: res => {
        for (let i = 0; i < res.data.length; i++) {
          res.data[i].categoryId = parseInt(this.data.categoryId)
          let photo = res.data[i].photo.split(",")
          if (photo[0] == "") {
            res.data[i].nophoto = 1
          } else {
            for (let o in photo) {
              photo[o] = app.imageUrl + photo[o]
            }
          }
          res.data[i].photo = photo
          let modifyTime = res.data[i].modifyTime,
            modifyDate = modifyTime.split(" ")[0].split("-")
          if (res.data[i].updateUserName) {
            res.data[i].updateInfo = "此记录已于当日" + modifyTime.substr(11, 2) + "时" + modifyTime.substr(14, 2) + "分被" + res.data[i].updateUserName + "修改"
          }
          if (res.data[i].status == 5) {
            res.data[i].updateInfo = `此记录已于${modifyDate[1]}月${modifyDate[2]}日${modifyTime.substr(11, 2)}时${modifyTime.substr(14, 2)}分被${res.data[i].updateUserName}处理`
          }

          if(res.data[i].video != ""){
            res.data[i].videoSrc = app.imageUrl + res.data[i].video 
          }
        }
        let timeDay = res.data[0].createTime.substr(0, 10).split("-"),
          timeMin = res.data[0].createTime.substr(11, 5).split(":"),
          checkTime = timeDay[0] + "年" + timeDay[1] + "月" + timeDay[2] + "日" + timeMin[0] + "时" + timeMin[1] + "分"
        
        this.setData({
          createUser: res.data[0].createUserName,
          phone: res.data[0].phone,
          time: checkTime,
          item: res.data[0].item,
          contentList: res.data
        })
        wx.hideLoading()
      },
      fail: res => {
        wx.showToast({
          title: '网络异常！',
          image: '/images/warning.png'
        })
      },
      complete: () => {
        this.setData({
          loading: 0
        })
      }
    })
  },




  previewImageDeal: function(e) {
    let detailIndex = parseInt(e.currentTarget.id.split('-')[0])
    let imageIndex = parseInt(e.currentTarget.id.split('-')[1])
    let current = this.data.contentList[detailIndex].photo
    wx.previewImage({
      current: current[imageIndex], // 当前显示图片的http链接
      urls: current // 需要预览的图片http链接列表
    })
  },



  updateInspectCondition: function(e) {
    let date = new Date();
    // console.log(this.data.contentList[e.currentTarget.id])
    let list = this.data.contentList[e.currentTarget.id]
    if (parseInt(date.getDate()) == parseInt(list.createTime.substr(8, 2)) && this.data.contentList[e.currentTarget.id].status != 5) {
      wx.showModal({
        content: "是否要对此排查内容进行修改？",
        success(res) {
          if (res.confirm) {
            wx.setStorage({
              key: "updateInspect" + list.id,
              data: list,
              success(res) {
                wx.navigateTo({
                  url: '../../pages/workInspectUpdate/workInspectUpdate?id=' + list.id,
                })
              },
              fail() {
                wx.showToast({
                  title: '异常错误',
                  image: '/images/warning.png'
                })
              }
            })


          }
        }
      })
    } else {
      wx.showToast({
        title: '超过规定修改时间！',
        icon: "none",
        mask: true
      })
    }

  },

  phoneCall: function() {
    wx.makePhoneCall({
      phoneNumber: this.data.phone
    })
  },

  notice: function() {
    this.data.hide == 0 ?
      this.setData({
        hide: 1
      }) :
      this.data.hide == 1 ? this.setData({
        hide: 2
      }) : this.setData({
        hide: 1
      })
  }

})